home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 11 / FM Towns Free Software Collection 11.iso / t_os / lib / yotpin / src / xbmload.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-22  |  4.1 KB  |  212 lines

  1. /*
  2. *    Yamana's Otomeza Plug-in Tool
  3. *        Xwindow Bit Map ローダ
  4. *    
  5. *    1993.11.17    DOS 用に作成
  6. *    1995.08.07    乙女座プラグイン化
  7. *    1995.08.08    あまりにも遅かったので一気に 320% ほど高速化(^^;)
  8. *    1995.08.21    圧縮XBMが乱れるのを修正
  9. *    
  10. */
  11.  
  12. #include    "otome_pi.h"
  13.  
  14. const char    longname[]    = "FILE  : XBM loader";
  15. const char    extend[]    = "XBM";
  16.  
  17. #define        USE_FILE    PI_LOAD
  18. #include    "otome_pi.c"
  19.  
  20.  
  21. char    *readbuf,*buf;
  22. char    *bufp;
  23.  
  24. /*************************************************************************/
  25.  
  26. #define    my_lower(c)        ( ((c)>'Z') ? (c): ((c)+' ') )
  27. #define    my_isdigit(c)    ( ((c) >='0' && (c)<= '9') ? 1:0 )
  28.  
  29. /* 英字のときチェックしない */
  30. #define    my_ishex(c)        ( ((c)>='0' && (c)<='9') ? \
  31.                                 ((c)-'0') : (my_lower(c)-'a'+10) )
  32.  
  33. int my_ishex2(int c)
  34. {    
  35.          if (c >='a' && c <='f')    return(1);
  36.     else if (c >='0' && c <='9')    return(1);
  37.     else if (c >='A' && c <='F')    return(1);
  38.     else                            return(0);
  39. }
  40.  
  41. /***********************/
  42.  
  43. #define        TMPMAX    80
  44.  
  45. int XBM_get_size( num,str,fp )
  46. int     *num;
  47. char    *str;
  48. FILE    *fp;
  49. {
  50.     char    tmp[ TMPMAX+1 ];
  51.     char    *p;
  52.     
  53.     while( !feof(fp) )
  54.     {    if(     fgets( tmp, TMPMAX ,fp    ) ==NULL )    return ERROR;
  55.         if( (p=strstr( tmp, "#define"    ))==NULL )    continue;
  56.         if( (p=strstr( p  , str         ))==NULL )    continue;
  57.         break;
  58.     }
  59.     /* 数字を探す */
  60.     while( *p !='\0' && strchr( "0123456789", *p )==NULL )    p++;
  61.     
  62.     *num = atoi( p );
  63.     return NOERR;
  64. }
  65.  
  66.  
  67. int XBM_getSize( size,fp )
  68. POINT    *size;
  69. FILE    *fp;
  70. {
  71.     int     i;
  72.     
  73.     if( XBM_get_size( &i,"width",fp )==ERROR || i<=0 )    return ERROR;
  74.     size->x = (short)i ;
  75.     
  76.     if( XBM_get_size( &i,"height",fp)==ERROR || i<=0 )    return ERROR;
  77.     size->y = (short)i ;
  78.     
  79.     return NOERR;
  80. }
  81.  
  82. /***********************/
  83.  
  84. char    xchg_bit( c )
  85. char    c;
  86. {
  87.     int     i;
  88.     char    tmp;
  89.     
  90.     for( i=0,tmp=0 ; i<8 ; i++, c>>=1 )
  91.         tmp |= ( (c&1) << (7-i) );
  92.     
  93.     return tmp;
  94. }
  95.  
  96. #define        LIMIT_COMM    " ,\r\n\t{};"
  97.  
  98. int XBM_load( image, size )
  99. char    *image;
  100. POINT    *size;
  101. {    
  102.     int     i,xs,bxs,val,pat;
  103.     int     off,all;
  104.     
  105.     /* 32ビット単位で扱われる    */
  106.     xs  = (size->x+ 7)>>3 ;                /*  8 */
  107.     bxs =((size->x+31)& 0xffe0)>>3 ;    /* 32 */
  108.     all = bxs * size->y ;
  109.     
  110.     pat = (bxs-xs) ;
  111.     
  112.     bufp = readbuf;
  113.     if( (bufp = strstr( bufp, "char"   ))==NULL )    return ERROR;
  114.     if( (bufp = strstr( bufp, "_bits[]"))==NULL )    return ERROR;
  115.     if( (bufp = strstr( bufp, "{"      ))==NULL )    return ERROR;
  116.     
  117.     while( *bufp !='\0' && strchr( LIMIT_COMM, *bufp ) )    bufp++;
  118.     off= 0;
  119.     while( off<all && bufp!=NULL )
  120.     {    
  121.         if( bufp[1] == 'x' || bufp[1] == 'X' )
  122.         {    val = (my_ishex( bufp[2] )<<4) | my_ishex( bufp[3] ) ;
  123.             bufp+=4;
  124.         }
  125.         else
  126.         {    /* 圧縮 XBM */
  127.             if( (bufp[1]>='0' && bufp[1]<='9') || 
  128.                 (bufp[1]>='a' && bufp[1]<='f') || 
  129.                 (bufp[1]>='A' && bufp[1]<='F')        )
  130.             {    val = (my_ishex( bufp[0] )<<4) | my_ishex( bufp[1] );
  131.                 bufp+=2;
  132.             }
  133.             else
  134.             {    val = my_ishex( bufp[0] );
  135.                 bufp+=1;
  136.             }
  137.         }
  138.         
  139.         image[ off++ ] = xchg_bit(val);
  140.         
  141.         if( pat && (off % bxs)>=xs )
  142.             for( i=0; i<pat; i++ )
  143.                 image[ off++ ] = 0;
  144.         
  145.         
  146.         while( *bufp!='\0' && strchr( LIMIT_COMM, *bufp ) )    bufp++;
  147.         
  148.     }
  149.     
  150.     return NOERR;
  151. }
  152.  
  153. long    getFileSize( fp )
  154. FILE    *fp;
  155. {
  156.     unsigned long    from,to;
  157.     
  158.     from = ftell(fp);
  159.     fseek( fp, 0, SEEK_END );
  160.     to = ftell(fp);
  161.     fseek( fp, from, SEEK_SET );
  162.     
  163.     return (to-from);
  164. }
  165.  
  166. /********************/
  167.  
  168. int APL_exec()
  169. {
  170.     FILE    *fp;
  171.     char    *image;
  172.     int     fsize;
  173.     POINT    size;
  174.     
  175.     if( (fp = fopen( PI_FNAME , "rb" )) == NULL )
  176.         return PI_ERROR_FILE_OPEN;
  177.     
  178.     if( XBM_getSize( &size, fp ) )
  179.     {    fclose(fp);
  180.         return ERROR;
  181.     }
  182.     
  183.     if( (readbuf=PI_MALLOC( (fsize=getFileSize(fp))+1 ))==NULL )
  184.     {    fclose(fp);
  185.         return PI_ERROR_NO_MEMORY;
  186.     }
  187.     
  188.     if( (image=PI_MALLOC( ((size.x+31)& 0xffe0)*size.y ))==NULL )
  189.     {    fclose(fp);
  190.         PI_FREE( readbuf );
  191.         return PI_ERROR_NO_MEMORY;
  192.     }
  193.     
  194.     fread( readbuf, 1,fsize, fp ), readbuf[fsize]='\0';
  195.     fclose(fp);
  196.     
  197.     if( XBM_load( image, &size ) )
  198.     {    PI_FREE( image );
  199.         PI_FREE( readbuf );
  200.         return ERROR;
  201.     }
  202.     PI_FREE( readbuf );
  203.     
  204.     pi_imge->image    = image;
  205.     pi_imge->pix    = 1;
  206.     pi_imge->size.x = size.x;
  207.     pi_imge->size.y = size.y;
  208.     
  209.     return NOERR;
  210. }
  211.  
  212.